﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionRoc.Core;
using VisionRoc.Tool;

namespace VisionRoc_Measure
{
    public partial class FormMindVisionTool : Form
    {
        MindVision m_MindVision;

        public FormMindVisionTool(MindVision _MindVision)
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景 
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲  
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.DoubleBuffered = true;//避免时间显示控件闪烁
            InitializeComponent();
            m_MindVision = _MindVision;
        }

       
        private void FormMindVisionTool_Load(object sender, EventArgs e)
        {
            m_MindVision.HalconWindow(hWindowControl1);
            m_MindVision.My_Camera.Read_Parameter();
            LoadTirggeModel();
            LoadSate();
            button3.Enabled = false;
            string result = "";
            Global.m_MultiLanguage.LControls = new List<Control>();
            Global.m_MultiLanguage.GetControls(this);
            if (!Global.m_MultiLanguage.LoadLanguage(this, out result))
            {
                MessageBox.Show(result + "\r\n程序当前语言可能异常，如果影响正常使用请重新启动程序", "加载语言", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 加载触发源
        /// </summary>
        public void LoadTirggeModel()
        {
            //判断触发模式为1 硬触发
            if (m_MindVision.My_Camera.TRIGGER_MODEL == 1)
            {
                // 触发 硬触发单选按钮事件
                uiRadioButton_HardTrigger.Checked = true;
            }
            // 判断触发模式为2 软触发
            if (m_MindVision.My_Camera.TRIGGER_MODEL == 2)
            {
                // 触发 软触发单选按钮事件
                uiRadioButton_SoftTrigger.Checked = true;
            }
            if (m_MindVision.My_Camera.TRIGGER_MODEL == 3)
            {
                // 触发 连续触发单选按钮事件
                radioButton_Continuous.Checked = true;
            }
        }


        /// <summary>
        /// 加载状态
        /// </summary>
        public void LoadSate()
        {
            switch (m_MindVision.My_Camera.CAMERA_STATE)
            {
                case 1:
                    uiRadioButton_Image.Checked = true;
                    break;
                case 2:
                    uiRadioButton_Path.Checked = true;
                    break;
                case 3:
                    uiRadioButton_OnLine.Checked = true;
                    break;
            }
        }

       

        /// <summary>
        /// 相机状态单选按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiRadioButton_Image_CheckedChanged_1(object sender, EventArgs e)
        {
            try
            {
                if (uiRadioButton_Image.Checked)
                {
                    m_MindVision.My_Camera.CAMERA_STATE = 1;
                    groupBox1.Enabled = true;
                    groupBox2.Enabled = false;
                    groupBox3.Enabled = false;
                    textBox_Image_Path.Text = m_MindVision.My_Camera.OFFLINE_IMAGE_PATH;
                    tool_CameraInfo.Text = "离线图像模式";
                    tool_CameraSN.Text = "NULL";
                    if (m_MindVision.My_Camera.OFFLINE_IMAGE_PATH != null)
                    {
                        HOperatorSet.ReadImage(out m_MindVision.Image, m_MindVision.My_Camera.OFFLINE_IMAGE_PATH);
                        //清除窗口缓存
                     
                        HTuple Width = new HTuple(), Height = new HTuple();
                        Width.Dispose(); Height.Dispose();
                        HOperatorSet.GetImageSize(m_MindVision.Image, out Width, out Height);
                        MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
                        m_MindVision.Image.DispObj(hWindowControl1.HalconWindow);
                    }
                }

                if (uiRadioButton_Path.Checked)
                {
                    m_MindVision.My_Camera.CAMERA_STATE = 2;
                    groupBox1.Enabled = false;
                    groupBox2.Enabled = true;
                    groupBox3.Enabled = false;
                    textBox_Image_File.Text = m_MindVision.My_Camera.OFFLINE_IMAGE_FILE_PATH;
                    tool_CameraInfo.Text = "离线图像路径模式";
                    tool_CameraSN.Text = "NULL";
                }
                if (uiRadioButton_OnLine.Checked)
                {
                    m_MindVision.My_Camera.CAMERA_STATE = 3;
                    groupBox1.Enabled = false;
                    groupBox2.Enabled = false;
                    groupBox3.Enabled = true;
                    textBox_Exposure.Text = m_MindVision.My_Camera.Exposure.ToString();
                    textBox_Gain.Text = m_MindVision.My_Camera.Gain.ToString();
                    tool_CameraInfo.Text = "在线相机";
                    tool_CameraSN.Text = m_MindVision.m_strSN;
                }
            }
            catch { }

        }

        /// <summary>
        /// 选择离线图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_SelectImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog oflie = new OpenFileDialog();
            //对话框显示的标题
            oflie.Title = "请打开需要的图片";
            //文件多选设置
            oflie.Multiselect = false;
            /*设置对话框的初始目录
            oflie.InitialDirectory = @"";*/
            //打开文件的类型"*.tif;*.tiff;*.gif;*.bmp;*.jpg;*.jpeg;*.png;*.pcx;*.pgm;*.ppm;*.pbm;*.xwd;*.ima,*;hobj
            oflie.Filter = "(*.tif;*.tiff;*.gif;*.bmp;*.jpg;*.jpeg;*.png;*.pcx;*.pgm;*.ppm;*.pbm;*.xwd;*.ima,*;hobj)|*.tif;*.tiff;*.gif;*.bmp;*.jpg;*.jpeg;*.png;*.pcx;*.pgm;*.ppm;*.pbm;*.xwd;*.ima,*;hobj)";
            oflie.ShowDialog();
            //获得选中文件的路经
            string opfpath = oflie.FileName;
            //判断拿到的路径是否为空
            if (opfpath == "")
            {
                //没有打开图像就结束本次程序
                return;
            }
            m_MindVision.My_Camera.OFFLINE_IMAGE_PATH = textBox_Image_Path.Text = opfpath;
            if (opfpath != null)
            {
                HOperatorSet.ReadImage(out m_MindVision.Image, opfpath);

                HTuple Width = new HTuple(), Height = new HTuple();
                Width.Dispose(); Height.Dispose();
                HOperatorSet.GetImageSize(m_MindVision.Image, out Width, out Height);
                MyHOperatorSet.SetHWindowPart(ref hWindowControl1, Width, Height);
                m_MindVision.Image.DispObj(hWindowControl1.HalconWindow);
            }
        }

        /// <summary>
        /// 清楚
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            textBox_Image_File.Text = m_MindVision.My_Camera.OFFLINE_IMAGE_PATH = "";
        }

        /// <summary>
        /// 选择离线路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_SelectPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog Save = new FolderBrowserDialog();
            Save.Description = "请选择文件夹路径";
            Save.ShowDialog();
            if (Save.SelectedPath != "")
            {
                textBox_Image_File.Text = Save.SelectedPath;
                m_MindVision.My_Camera.OFFLINE_IMAGE_FILE_PATH = textBox_Image_File.Text;
                m_MindVision.My_Camera.LoPath();
            }
        }

        /// <summary>
        /// 清楚
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            textBox_Image_Path.Text = m_MindVision.My_Camera.OFFLINE_IMAGE_FILE_PATH = "";
        }

        /// <summary>
        /// 设置软触发或外触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiRadioButton_SoftTrigger_CheckedChanged(object sender, EventArgs e)
        {
            if (uiRadioButton_SoftTrigger.Checked)
            {
                // 设置软触发
                m_MindVision.TriggerMode = "On";
                m_MindVision.My_Camera.TRIGGER_MODEL = 2;
                button_Trigger.Enabled = true;
                button_Continuous.Enabled = false;
                button3.Enabled = false;
            }
            if (uiRadioButton_HardTrigger.Checked)
            {
                //设置外触发
                m_MindVision.TriggerMode = "Line1";
                m_MindVision.My_Camera.TRIGGER_MODEL = 1;
                button_Trigger.Enabled = true;
                button_Continuous.Enabled = false;
                button3.Enabled = false;
            }
            if (radioButton_Continuous.Checked)
            {
                //设置连续触发
                m_MindVision.TriggerMode = "Off";
                m_MindVision.My_Camera.TRIGGER_MODEL = 3;
                button_Trigger.Enabled = false;
                button_Continuous.Enabled = false;
                uiRadioButton_HardTrigger.Enabled = false;
                uiRadioButton_SoftTrigger.Enabled = false;
                button3.Enabled = true;
                m_MindVision.Continuous(true);
            }
        }

        private void button_Set_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_Exposure.Text == "") return;
                if (textBox_Gain.Text == "") return;
                m_MindVision.Set_Exposure(Convert.ToDouble(textBox_Exposure.Text));
                m_MindVision.Set_Gain(Convert.ToInt32(textBox_Gain.Text));
                m_MindVision.My_Camera.Exposure = Convert.ToDouble(textBox_Exposure.Text);
                m_MindVision.My_Camera.Gain = Convert.ToInt32(textBox_Gain.Text);
                MessageBox.Show("设置成功。");
            }
            catch { MessageBox.Show("曝光 增益设置失败！"); }
        }

        /// <summary>
        /// 实时采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Continuous_Click(object sender, EventArgs e)
        {
            m_MindVision.Continuous(true);
            uiRadioButton_HardTrigger.Enabled = false;
            uiRadioButton_SoftTrigger.Enabled = false;
            button_Continuous.Enabled = false;
            button3.Enabled = true;
        }

        /// <summary>
        /// 停止实时采集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            m_MindVision.Continuous(false);
            button_Continuous.Enabled = true;
            button3.Enabled = false;
            uiRadioButton_HardTrigger.Enabled = true;
            uiRadioButton_SoftTrigger.Enabled = true;

        }

        private void button_Trigger_Click(object sender, EventArgs e)
        {
            m_MindVision.Trigger();
        }

        private void FormMindVisionTool_FormClosing(object sender, FormClosingEventArgs e)
        {
            //判断 如果连续采集按钮没有停止就关闭当前窗体 
            if (button_Continuous.Text == "停止")
            {
                // 则停止连续采集
                m_MindVision.Continuous(false);
            }


            bool shouldClose = CheckSomeCondition();
            if (!shouldClose)
            {
                // 条件不满足，不关闭窗体
                e.Cancel = true;

            }

            DialogResult res = MessageBox.Show("是否保存当前数据？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (res == DialogResult.OK)  //1为确定，2为取消,...
            {
                try
                {

                    m_MindVision.My_Camera.Write_Parameter();
                    MessageBox.Show("保存数据成功");
                    //MessageBox.Show("保存失败,请登录工程师权限,窗体关闭", "警告!", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand);
                }
                catch { MessageBox.Show("保存失败！"); }
            }
        }

        // 检查条件的示例方法
        private bool CheckSomeCondition()
        {
            if (radioButton_Continuous.Checked)
            {
                MessageBox.Show("关闭窗体前不能选择连续模式");
                return false;
            }
            else
            {
                return true;
            }
            // 在这里实现你的条件检查逻辑
            // 例如：返回 false 以模拟条件不满足

        }

        private void hSmartWindowControl1_HMouseMove(object sender, HMouseEventArgs e)
        {
            tool_ImageInfo.Text = MyHOperatorSet.MouseMove(m_MindVision.Image, e);
        }


    }
}
